package yzpln.lanou.com.iamsinger.activity;

import android.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

import yzpln.lanou.com.iamsinger.R;
import yzpln.lanou.com.iamsinger.fragment.friend.FriendFragment;
import yzpln.lanou.com.iamsinger.fragment.more.MoreFragment;
import yzpln.lanou.com.iamsinger.fragment.myrunning.MyRunningFragmen;
import yzpln.lanou.com.iamsinger.fragment.runningspeak.RunningSpeakFragment;
import yzpln.lanou.com.iamsinger.fragment.startrunning.StartRunningFragment;

public class MainActivity extends BaseActivity {
    private TabHost th;
    private TabSpec myRunning_page, runningSpeak_page, startRunning_page, friend_page,
            more_page;
    private View myRunning_view, runningSpeak_view, startRunning_view, frinend_view, more_view;
    private android.support.v4.app.FragmentManager fm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fm = getSupportFragmentManager();
        initView();
        showTabHost();
        initFragment();
    }

    @Override
    int getView() {
        return R.layout.activity_main;
    }

    @Override
    View initView() {
        th = (TabHost) findViewById(android.R.id.tabhost);
        th.setup();
        return null;
    }

    @Override
    View initData() {
        return null;
    }

    private void showTabHost() {
        setTabHost(getTabSpec(myRunning_page, "home"),
                getPageView(myRunning_view, R.layout.myrunning_indicator),
                R.id.myRunning_page);
        setTabHost(getTabSpec(runningSpeak_page, "centent"),
                getPageView(runningSpeak_view, R.layout.runningspeak_indicator),
                R.id.runningSpeak_page);
        setTabHost(getTabSpec(startRunning_page, "question"),
                getPageView(startRunning_view, R.layout.startrunning_indicator),
                R.id.startRunning_page);
        setTabHost(getTabSpec(friend_page, "things"),
                getPageView(frinend_view, R.layout.friend_indicator),
                R.id.friend_page);
        setTabHost(getTabSpec(more_page, "person"),
                getPageView(more_view, R.layout.more_indicator),
                R.id.more_page);

    }

    private TabSpec getTabSpec(TabSpec spec, String text) {
        spec = th.newTabSpec(text);
        return spec;
    }

    private View getPageView(View view, int anchor) {
        view = getLayoutInflater().inflate(anchor, null);
        return view;
    }

    private void setTabHost(TabSpec spec, View view, int anthor) {
        spec.setIndicator(view);
        spec.setContent(anthor);
        th.addTab(spec);
    }

    private void initFragment() {
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.myRunning_page, new MyRunningFragmen());
        ft.replace(R.id.runningSpeak_page, new RunningSpeakFragment());
        ft.replace(R.id.startRunning_page, new StartRunningFragment());
        ft.replace(R.id.friend_page, new FriendFragment());
        ft.replace(R.id.more_page, new MoreFragment());
        ft.commit();
    }
}